import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-zhongyaoyinpian',
  templateUrl: './zhongyaoyinpian.component.html',
  styleUrls: ['./zhongyaoyinpian.component.scss']
})
export class ZhongyaoyinpianComponent implements OnInit {
  drugName = '';
  guijing = [];
  guijingList = [];
  xingwei = [];
  xingweiList = [];
  messages: any = [];
  currentPage = 1;
  targetPage = 1;
  total_pages: any;
  total_results: any;
  displayData = [];
  allChecked = false;
  indeterminate = false;

  constructor(private http: HttpClient, private router: Router) { }
  /**
  * @desc 获取列表数据
  */
  getList() {
    const gj = this.guijing.join(' ');
    const xw = this.xingwei.join(' ');
    const params = {
      params: {
        'name': this.drugName || '',
        'gj': gj || '',
        'xw': xw || '',
        'per_page': 20,
        'current_page': this.currentPage
      } as any
    };
    this.http.get(window.API_ROOT + 'api/data/zyyps', params).toPromise().then((res: any) => {
      const result = res.data;
      this.messages = result.results;
      this.allChecked = false;
      this.indeterminate = false;
      if (this.messages.length === 0 && this.currentPage !== 1) {
        this.currentPage = 1;
        this.getList();
      } else {
        this.total_pages = result.total_page;
        this.total_results = result.total_num;
      }

    });
  }
  refreshStatus(): void {
    const allChecked = this.displayData.length > 0 ? this.displayData.filter(value => !value.disabled).every(value => value.checked === true) : false;
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    this.refreshStatus();
  }
  /**
   * @description 跳转页数并搜索
   */
  JumpToTargetPage() {
    if (this.targetPage < 1) {
      this.targetPage = 1;
    }
    if (this.targetPage > this.total_pages) {
      this.targetPage = this.total_pages;
    }
    this.currentPage = this.targetPage;
    this.getList();
  }

  /**
   * @description 数据改变回调(ng-valley)
   * @param ev message[]
   */
  messageChanged(messages) {
    this.displayData = messages;
  }
  /**
   * @description 跳转上一页
   */
  goToBeforePage() {
    if (this.currentPage === 1) {
      return;
    }
    this.currentPage--;
    this.getList();

  }
  /**
   * @description 跳转下一页
   */
  goToNextPage() {
    if (this.currentPage === this.total_pages) {
      return;
    }
    this.currentPage++;
    this.getList();
  }
  /**
   * @description 跳转详情
   */
  goToDetail(id) {
    // window.open('/#/main/basic-database/detail');
    const type = '中药饮片';
    this.router.navigate(['main/basic-database/detail', id, type]);
  }

  /**
   * @description 归经选项搜索
   */
  searchGuijing(searchText) {
    const params = {
      params: {
        'value': searchText
      } as any
    };
    this.http.get(window.API_ROOT + 'api/recipes/guijings', params).toPromise().then((res: any) => {
      this.guijingList = res.data;
    });
  }
  /**
   * @description 性味选项搜索
   */
  searchXingwei(searchText) {
    const params = {
      params: {
        'value': searchText
      } as any
    };
    this.http.get(window.API_ROOT + 'api/recipes/xingweis', params).toPromise().then((res: any) => {
      this.xingweiList = res.data;
    });
  }

  /**
   * @description 检索
   */
  search() {
    this.currentPage = 1;
    this.getList();
  }

  ngOnInit() {
    this.getList();
  }

}
